//
//  PlayViewController.m
//  随拍
//
//  Created by lanou3g on 16/1/19.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "PlayViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface PlayViewController ()

@property(nonatomic,strong)MPMoviePlayerController *movieplayer; //创建视频播放器

@end

@implementation PlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.movieplayer play];
    
    [self Notification];
    
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(NSURL *)getNetWorkUrl:(NSString *)strurl{
    NSString *urlstr =strurl ;
    
    NSURL *url = [NSURL URLWithString:[urlstr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    return url;
    
    
    
    
    
}

-(MPMoviePlayerController *)movieplayer{
    if (! _movieplayer) {
        NSURL *url = [self  getNetWorkUrl:_strurl];
        
        
        _movieplayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
        
        _movieplayer.view.frame = self.view.bounds;
        
        _movieplayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        
        [self.view addSubview:_movieplayer.view];
        
        
    }
    
    return _movieplayer;
}


//添加通知
-(void)Notification{
    NSNotificationCenter *notification = [NSNotificationCenter defaultCenter];
    
    [notification addObserver:self selector:@selector(changeClick:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.movieplayer];
    
    
    [notification addObserver:self selector:@selector(endPlay:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.movieplayer];
    
    
    
    
}


-(void)changeClick:(NSNotification *)notification{
    switch (self.movieplayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暂停播放");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"停止播放");
            break;
            
        default:
            
            NSLog(@"播放状态%ld",self.movieplayer.playbackState);
            
            break;
    }
}

-(void)endPlay:(NSNotification *)notification{
    NSLog(@"播放状态%ld",self.movieplayer.playbackState);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
